/**
 * 
 * @param {String|Document} elem 
 * @param { Object} options 
 */
function Page(elem, options,callback) {
    this.callback=callback;
    //使用容器保存dom对象
    this.elem = (Object.prototype.toString.call(elem) == '[object String]') ? document.querySelector(elem) : elem;
    this.inputDom = null;
    this.listDom = null;
    this.initialFigure = {
        language: {
            firstPage: '<<',
            previousPage: '<',
            list: '',
            nextPage: '>',
            lastPage: '>>'
        },
        data: {
            total: 100,//数据总条数
            pageSize: 10//每页的数量
        }
    }
    this.mergeFigure(options);
    this.totalPage = Math.ceil(this.initialFigure.data.total / this.initialFigure.data.pageSize);
    this.currentPage = 1;
    this.elementAdd();
    this.executeEvent();

}
//传参修改默认值
Page.prototype.mergeFigure = function (options) {
    for (let key in this.initialFigure.language) {
        options.language[key] && (this.initialFigure.language[key] = options.language[key])
    }
    for (let key in this.initialFigure.data) {
        options.data[key] && (this.initialFigure.data[key] = options.data[key])
    }
}
//元素的添加
Page.prototype.elementAdd = function () {
    this.elem.innerHTML = '';
    this.createOutside();
    this.createNumber();
    this.createSearchElement();
    console.log('elementAdd=代码执行了吗？', this.totalPage)
    Object.prototype.toString.call(this.callback) == '[object Function]' && this.callback(this.currentPage);
}
//创建外层div的方法
Page.prototype.createOutside = function () {
    for (let key in this.initialFigure.language) {
        let divDom = document.createElement('div');
        // 设置样式名称 后期用户需要针对分页进行样式控制 可以自己根据样式名称编写样式
        divDom.className = key;
        // 如果是存储数字页的容器使用对象属性保存 保存的目的是为了方便后期使用(避免在页面上寻找list的元素)
        key == 'list' && (this.listDom = divDom);
        divDom.innerHTML = this.initialFigure.language[key];
        console.log(this.elem);
        this.elem.appendChild(divDom);
    }
}
//创建中间数字的
Page.prototype.createNatureNumber = function (start, end) {
    console.log('代码执行了吗？')
    for (let i = start; i <= end; i++) {
        let pDom = document.createElement('p');
        i == this.currentPage && (pDom.className = 'active')
        pDom.innerHTML = i;
        this.listDom.appendChild(pDom);
        console.log(this.listDom);
    }
}
//传入对应参数-----------------------------------备注：待会儿，重新写，现在写，效果凸显不出来
Page.prototype.createNumber = function () {
    if (this.totalPage <= 5) {
        this.createNatureNumber(1, this.totalPage);
    } else {
        if (this.currentPage <= 3) {
            this.createNatureNumber(1, 5);
        } else if (this.currentPage >= this.totalPage - 2) {
            this.createNatureNumber(this.totalPage - 4, this.totalPage);
        } else {
            this.createNatureNumber(this.currentPage - 2, this.currentPage + 2);
        }
    }
}
//点击事件绑定函数
Page.prototype.executeEvent = function () {
    this.elem.addEventListener('click', event => {
        console.log(this.elem);
        switch (event.target.className) {
            case 'firstPage':
                this.currentPage = 1;
                this.elementAdd();
                break;
            case 'previousPage':
                if (this.currentPage > 1) {
                    this.currentPage -= 1;
                    this.elementAdd();
                }
                break;
            case 'nextPage':
                if (this.currentPage < this.totalPage) {
                    this.currentPage += 1;
                    this.elementAdd();
                }
                break;
            case 'lastPage':
                this.currentPage = this.totalPage;
                this.elementAdd();
                break;
            default:
                if (event.target.nodeName == 'P') {
                    this.currentPage = Number(event.target.innerHTML);
                    this.elementAdd();
                } else if (event.target.nodeName == 'BUTTON') {
                    // 获取到当前输入的值
                    let num = parseInt(this.inputDom.value);
                    if (num > 0 && num < this.totalPage) {
                        this.currentPage = num;
                        this.elementAdd();
                    }
                }
        }
    })
}
//添加搜索事件
Page.prototype.createSearchElement = function () {
    this.inputDom = document.createElement('input');
    this.inputDom.style.width = '60px';
    this.inputDom.style.marginRight = '20px';
    this.elem.appendChild(this.inputDom);
    console.log('代码执行了吗？')
    let btnDom = document.createElement('button');
    btnDom.innerHTML = 'seach';
    this.elem.appendChild(btnDom);
    this.elem.addEventListener('click', event => {
    })
}